package controleabastecimento2.activies;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import controleabastecimento2.model.Veiculo;
import controleabastecimento2.model.Veiculo.Veiculos;

public class EditarVeiculo extends Activity {
	static final String GASOLINA = "Gasolina", ALCOOL = "Alcool", DIESEL = "Diesel";
	
	static final int RESULT_SALVAR = 1;
	static final int RESULT_EXCLUIR = 2;
	
	private EditText edtVeiculo, edtPlaca;
	private RadioButton rdbGasolina, rdbAlcool, rdbDiesel;
	private Button btnAtualizar, btnDeletar, btnCancelar;
	private Integer id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editar_veiculo);
		
		edtPlaca =(EditText)findViewById(R.id.edtPlaca);
		edtVeiculo = (EditText)findViewById(R.id.edtVeiculo);
		rdbGasolina = (RadioButton)findViewById(R.id.rdbGasolina);
		rdbAlcool = (RadioButton)findViewById(R.id.rdbAlcool);
		rdbDiesel = (RadioButton)findViewById(R.id.rdbDiesel);
		btnAtualizar = (Button)findViewById(R.id.btnAtualizar);
		btnDeletar = (Button)findViewById(R.id.btnDeletar);
		btnCancelar =(Button)findViewById(R.id.btnCancelar);
		id = null;
		
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			id=extras.getInt(Veiculos.CODIGO);
		} 
		if (id != null){
			Veiculo v = buscarVeiculo(id);
			edtPlaca.setText(v.getPlaca());
			edtVeiculo.setText(v.getModelo());
			if(v.getCombustivel().equals(GASOLINA)){
				rdbGasolina.isSelected();
			} else if(v.getCombustivel().equals(ALCOOL)){
				rdbAlcool.isSelected();
			} else {
				rdbDiesel.isSelected();
			}
			
		}
		
		btnCancelar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
				
			}
		});
		
		btnAtualizar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
					salvar();
				
			}
		});
		
		if (id == null) {

			btnDeletar.setVisibility(View.INVISIBLE);
		} else {
		btnDeletar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				deletar();

			}
		});
	}
		
}
	
	@Override
	protected void onPause() {
		super.onPause();

		setResult(RESULT_CANCELED);
		finish();
	}
	
	
	public void salvar() {
		
		Veiculo veiculo = new Veiculo();
		if (id != null) {
			veiculo.setCodigo(id);
		}

		veiculo.setModelo(edtVeiculo.getText().toString());
		veiculo.setPlaca(edtPlaca.getText().toString());
		if(rdbGasolina.isSelected()){
			veiculo.setCombustivel(GASOLINA);
		} else if(rdbAlcool.isSelected()){
			veiculo.setCombustivel(ALCOOL);
		} else{
			veiculo.setCombustivel(DIESEL);
		}
		salvarVeiculo(veiculo);
		setResult(RESULT_OK, new Intent());
		finish();

	}
	
	public void deletar() {
		if (id != null) {
			deletarVeiculo(id);
		}

		setResult(RESULT_OK, new Intent());
		finish();
	}
	
	protected Veiculo buscarVeiculo(int codigo) {
		return ListarVeiculo.daoVeiculo.buscarVeiculo(codigo);
	}
	

	protected void salvarVeiculo(Veiculo veiculo) {
		ListarVeiculo.daoVeiculo.salvar(veiculo);
	}


	protected void deletarVeiculo(int codigo) {
		ListarVeiculo.daoVeiculo.deletar(codigo);
	
	}
}